<template>
  <div class="app-container">
    <cus-wraper>
      <cus-filter-wraper>
        <div>
          <el-input v-model="listQuery.id" placeholder="请输入分组Id" style="width:200px;margin: 0 10px 10px 0;" clearable></el-input>
          <el-input v-model="listQuery.number" placeholder="请输入分组数量" style="width:200px;margin: 0 10px 10px 0;" clearable></el-input>
          <el-input v-model="listQuery.company" placeholder="请输入分组单位" style="width:200px;margin: 0 10px 10px 0;" clearable></el-input>
          <el-select filterable v-model="listQuery.isDisable" class="filter-item" placeholder='请选择是否禁用' style="width: 200px;margin: 0 10px 10px 0;">
            <el-option v-for="item in flagList2" :key="item.key" :label="item.lab" :value="item.key"></el-option>
          </el-select>
          <el-button type="primary" @click="search" icon="el-icon-search">查询</el-button>
          <el-button type="info" @click="reGetList" icon="el-icon-search">重置</el-button>
        </div>
        <el-button type="primary" @click="handleCreate" icon="el-icon-plus" style="max-height: 36px;">添加</el-button>
      </cus-filter-wraper>
      <div class="table-container">
        <el-table v-loading="listLoading" :data="list" size="mini" fit element-loading-text="Loading"
                  highlight-current-row >
          <el-table-column label="ID" prop="id" align="center"></el-table-column>
          <el-table-column label="数量" prop="number" align="center"></el-table-column>
          <el-table-column label="单位" prop="company" align="center"></el-table-column>
          <el-table-column label="状态" prop="isDisable" align="center">
            <template slot-scope="scope">
              <el-button size="mini" type="success" v-if="scope.row.isDisable === 0" round>已启用</el-button>
              <el-button size="mini" type="danger" v-if="scope.row.isDisable === 1" round>已禁用</el-button>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createdTime" align="center"></el-table-column>

          <el-table-column  align="right" v-if="this.global_checkBtnPermission(['sys:user:edit','sys:user:delete'])" label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" @click="handleUpdate(scope.row)" icon="el-icon-edit" plain>
                {{scope.row.isDisable === 0?'禁用':'启用'}}
              </el-button>
              <cus-del-btn @ok="handleDelete(scope.row)"/>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="getList"/>
      </div>

      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogVisible" width="40%" v-dialogDrag @close="handleDialogClose">
        <el-form ref="dataForm" :model="form" :rules="rules" label-width="80px" class="demo-ruleForm">
          <el-form-item label="数量:" prop="number">
            <el-input v-model="form.number"  onkeyup="value=value.replace(/[^\d]/g,'')"></el-input>
          </el-form-item>
          <el-form-item label="单位:" prop="company">
            <el-input v-model="form.company" ></el-input>
          </el-form-item>
          <el-form-item label="状态:" prop="groupOrder">
            <el-radio v-model="form.isDisable" label='0'>启用</el-radio>
            <el-radio v-model="form.isDisable" label='1'>禁用</el-radio>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false"> 取 消 </el-button>
          <el-button type="primary" @click="submitForm"> 确 认 </el-button>
        </span>
      </el-dialog>
    </cus-wraper>
  </div>
</template>

<script>
  import { getRoomPage, saveSysUser, deleteSysUser, updateCompanyDictionaries } from '@/api/dictionaries/dictionaries'

  export default {
    data() {
      return {
        dialogVisible: false,
        list: [],
        listLoading: true,
        total: 0,
        listQuery: {
          page: 1,
          pageSize: 10,
          id: undefined,
          number: undefined,
          company: undefined,
          isDisable: undefined,
        },
        input: '',
        form: {
          labelName: undefined, //主键ID
          isDisable:'0',
        },
        flagList: [{ key: '1', lab: 'id' },{ key: '2', lab: '数量' },{ key: '3', lab: '单位' }, { key: '4', lab: '状态' }],
        flagList2: [{ key: '0', lab: '启用' }, { key: '1', lab: '禁用' } ],
        flagIndex:'1',
        flagText:'',
        flagText2:'',
        searchVal:'',
        dialogStatus: '',
        titleMap: {
          update: '编辑',
          create: '创建'
        },
        rules: {
          labelName: [
            {required: true, message: '请输入名称', trigger: 'blur'},
          ]
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true;
        getRoomPage(this.listQuery).then(response => {
          // response)
          this.list = response.data.records
          this.total = response.data.total
          this.listLoading = false
        })
      },
      search(){
        this.getList();
      },
      reGetList(){
        this.searchVal = ''
        this.listQuery.id = undefined
        this.listQuery.number = undefined
        this.listQuery.company = undefined
        this.listQuery.isDisable = undefined
        this.getList();
      },
      handleCreate() {
        this.resetForm()
        this.dialogStatus = 'create'
        this.dialogVisible = true
      },
      handleUpdate(row) {

        let url ='?id=' + row.id  + '&isDisable=' + (row.isDisable === 0?1:0)

        updateCompanyDictionaries(url).then(response => {
          if (response.code === 0) {
            this.getList()
            this.$notify({
                    type:'success',
                    message: '操作成功',
                    position: 'bottom-right',
                    showClose: false,
                    duration: 3000
                });
          }
        })
      },
      handleDelete(row) {
        let id = [row.id]
        deleteSysUser(id).then(response => {
          if (response.code === 0) {
            this.getList()
            this.$notify({
                    type:'success',
                    message: '操作成功',
                    position: 'bottom-right',
                    showClose: false,
                    duration: 3000
                });
          }
        })
      },
      submitForm() {
        this.$refs.dataForm.validate(valid => {
          if (valid) {
            saveSysUser(this.form).then(response => {
              if (response.code === 0) {
                this.getList()
                this.$notify({
                    type:'success',
                    message: '操作成功',
                    position: 'bottom-right',
                    showClose: false,
                    duration: 3000
                });
                this.dialogVisible = false
              } else {
                this.$message({
                message: response.message,
                type: 'error'
            });
              }
            }).catch(err => {

            })
          }
        })
      },
      resetForm() {
        this.form = {
          id: undefined, //主键ID
          isDisable:'0',
        }
      },
      // 监听dialog关闭时的处理事件
      handleDialogClose() {
        if (this.$refs['dataForm']) {
          this.$refs['dataForm'].clearValidate() // 清除整个表单的校验
        }
      }
    }
  }
</script>
<style lang="scss" scoped>
  .search-line{
    margin-bottom: 10px;
    padding: 10px;
    background: white;
  }
  .filter-wraper{
    display: flex;
    justify-content: space-between;
  }
  .table-container /deep/ .el-table .cell{
    display:flex;
  }
</style>
